/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.main;
import javax.swing.*;
import java.awt.Image;
import java.awt.event.*;
import model.utility.ImageUtility;
import model.game.*;
import model.creature.moving_creature.Direction;
import model.creature.moving_creature.Player;
import java.util.ArrayList;
import java.lang.Math;

/**
 *
 * @author Satrio Dewantono
 */
public class BackGround {
  private int dx = 0;
  private int dy = 0;
  private int x;
  private int y;
  private static final int playeroffsetx = 175;
  private static final int playeroffsety = 145;
  private int width;
  private int height;
  private int player_abs_x,player_abs_y;
  private Image image;
  private InformationLayer il_;
  private GameEngine game_;
  private Player player;
  private ArrayList<ArrayList<Image>> drawlist;

    public BackGround(GameEngine eng) {
        game_ = eng;
	il_ = new InformationLayer(game_);
	player = game_.getCreatures().getPlayer();
	//drawlist = new ArrayList<ArrayList<Image>>();
	makeDrawList();
        player_abs_x = game_.getConstants().getPlayerStartX();
        player_abs_y = game_.getConstants().getPlayerStartY();
	x = -player_abs_x*25+playeroffsetx;
	y = -player_abs_y*25+playeroffsety;
    }

    public void makeDrawList(){
      int i;
      int j;
      width = il_.getCurrentMapColumnsCount();
      height = il_.getCurrentMapRowsCount();
      drawlist = new ArrayList<ArrayList<Image>>();
      for (i = 0;i<=width-1;i++){
	drawlist.add(new ArrayList<Image>());
	for (j = 0;j<=height-1;j++){
	  drawlist.get(i).add(il_.getCellAt(i,j).getImage());
	}
      }
    }

    public ArrayList<ArrayList<Image>> getDrawList(){
      return drawlist;
    }

    public void move() {
        x += dx;
        y += dy;
	player_abs_x = Math.abs((x-playeroffsetx)/25);
	player_abs_y = Math.abs((y-playeroffsety)/25);
	if (il_.getCellAt(player_abs_x,player_abs_y) == player.getCellInFrontOffSelf()){
	   player.moveForward();
	}
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getDX(){
      return dx;
    }

    public int getDY(){
      return dy;
    }

    public void resetPos(){
      player_abs_x = game_.getConstants().getPlayerStartX();
      player_abs_y = game_.getConstants().getPlayerStartY();
      x = -player_abs_x*25+playeroffsetx;
      y = -player_abs_y*25+playeroffsety;
    }

    public void setAbsX(int newx){
      player_abs_x = newx;
      x = -player_abs_x*25+playeroffsetx;
    }

    public void setAbsY(int newy){
      player_abs_y = newy;
      y = -player_abs_y*25+playeroffsety;
    }

    public int getabsX(){
      return player_abs_x;
    }

    public int getabsY(){
      return player_abs_y;
    }

    public Image getImage() {
        return image;
    }

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
	  if ((player.getDirection() == Direction.kLeft) && (player.canMoveForward())){
            dx = 2;
	    dy = 0;
	  }else{
	    dx = 0;
	  }
        }

        if (key == KeyEvent.VK_RIGHT) {
	  if ((player.getDirection() == Direction.kRight) && (player.canMoveForward())){
            dx = -2;
	    dy = 0;
	  }else{
	    dx = 0;
	  }
        }

        if (key == KeyEvent.VK_UP) {
	  if ((player.getDirection() == Direction.kUp) && (player.canMoveForward())){
            dy = 2;
	    dx = 0;
	  }else{
	    dy = 0;
	  }
        }

        if (key == KeyEvent.VK_DOWN) {
	  if ((player.getDirection() == Direction.kDown) && (player.canMoveForward())){
            dy = -2;
	    dx = 0;
	  }else{
	    dy = 0;
	  }
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }
}
